define(function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        var _this = this;
        return {
          collapseValue: ['1', '4'],
          collapse2FormDisable: true,
          urls: {
            index: backend.workflowService + '/api/bench/done',
            runInsInfo: backend.workflowService + '/api/core/myinstanceInfo',
            endInsInfo: backend.workflowService + '/api/core/myinstanceInfoHis',
            getComments: backend.workflowService + '/api/core/getAllComments/',
            hang: backend.workflowService + '/api/core/hangup',
            wake: backend.workflowService + '/api/core/wakeup',
            tackBack: backend.workflowService + '/api/core/tackback',
            urge: backend.workflowService + '/api/core/urge',
            resetNodeUser: backend.workflowService + 'api/core/resetNodeUser',
            tackbackToFirst: backend.workflowService + '/api/core/tackbackToFirst',
            activate: backend.workflowService + 'api/core/activate'
          },
          height: yufp.frame.size().height - 103,
          currentOrgId: yufp.session.org.id,
          currentUserId: yufp.session.user.loginCode,
          returnBackFuncId: '',
          returnBackRootId: '',
          instanceIdInfo: {
            instanceId: '',
            flowName: '',
            flowId: '',
            flowStarter: '',
            flowStarterName: '',
            startTime: '',
            systemId: '',
            orgId: '',
            flowState: '',
            bizId: '',
            bizUserName: '',
            bizUserId: '',
            lastNodeId: '',
            lastNodeName: '',
            nodeId: '',
            nodeSign: '',
            nodeName: '',
            nodeState: '',
            nodeType: '',
            handleType: '',
            bizPage: '',
            opType: {}
          },
          commentInfo: {
            instanceId: '',
            nodeId: '',
            userId: '',
            commentId: '',
            commentSign: 'O-12',
            userComment: '',
            ext: ''
          },
          commentRules: {
            userComment: [
              { required: true, message: '请填写意见', trigger: 'blur' }
            ]
          },
          flowParam: [],
          button: { // 按钮权限控制
            hungUp: '0',
            tackBack: '0',
            urged: '0',
            recall: '0',
            activate: '0'
          },
          UserSelect: {
            reset: '重置节点办理人员选择',
            showUserDialog: false,
            userUrl: backend.workflowService + '/api/org/users',
            Users: [],
            SelectUsers: []
          },
          nwfbiztypePage: 'nwfbiztypePage',
          title: 'reset',
          type: '',
          nodeHistory: [],
          commentinfo: '',
          isShow: 0,
          pagesize: 10,
          currentPage: 1,
          total: 0
        };
      },
      mounted: function () { // 进入初始化
        this.instanceInfoFn(data);
        this.type = data.type;
        this.instanceIdInfo.instanceId = data.instanceId;
        this.returnBackFuncId = data.returnBackFuncId;
        this.returnBackRootId = data.returnBackRootId;
        if (data.isShow != null) {
          this.isShow = data.isShow;
        }
      },
      methods: {
        tabClick: function (tab, event) {
          var _this = this;
          var url = _this.urls.getComments;
          if (tab.name == 'second') {
            yufp.service.request({
              method: 'GET',
              url: url,
              data: { mainInstanceId: _this.instanceIdInfo.mainInstanceId },
              callback: function (code, message, response) {
                if (response.code == 0) {
                  if (response.data != null ? response.data.length > 0 : false) {
                    _this.commentinfo = '';
                    var nodeHistory = response.data;
                    // commentSign翻译
                    for (var i = 0, length = nodeHistory.length; i < length; i++) {
                      nodeHistory[i].commentSign = yufp.lookup.convertKey('OP_TYPE', nodeHistory[i].commentSign);
                      // 时间格式化
                      // nodeHistory[i].startTime = yufp.util.dateFormat(nodeHistory[i].startTime, '{y}-{m}-{d}');
                    }
                    _this.nodeHistory = nodeHistory;
                  } else {
                    _this.commentinfo = '暂无审批历史数据';
                  }
                }
              }
            });
          }
        },
        instanceInfoFn: function (param) { // 初始化实例信息
          var _this = this;
          var url = '';
          var params = { instanceId: param.instanceId };
          if (param.nodeId != null) {
            params.nodeId = param.nodeId;
          }
          if (param.type == 'DONE') {
            url = _this.urls.runInsInfo;
          } else if (param.type == 'HIS') {
            url = _this.urls.endInsInfo;
          }
          yufp.service.request({
            method: 'GET',
            url: url,
            data: params,
            callback: function (code, message, response) {
              if (response.code == 0) {
                _this.instanceIdInfo = response.data;
                _this.instanceIdInfo.nodeId = param.nodeId;
                if (response.data.param != null) { // 流程参数初始化
                  for (var key in response.data.param) {
                    var paramTT = {};
                    paramTT.key = key;
                    paramTT.value = response.data.param[key];
                    _this.flowParam.push(paramTT);
                  }
                }
                if (response.data.comment != null) { // 设置评论反显
                  _this.commentInfo = response.data.comment;
                }
                _this.commentInfo.instanceId = _this.instanceIdInfo.instanceId;
                _this.commentInfo.userId = _this.currentUserId;
                _this.commentInfo.nodeId = _this.instanceIdInfo.nodeId;
                // 按钮权限
                var button = _this.instanceIdInfo.opType;
                if (button != null) {
                  if (param.button != null) {
                    for (var key in _this.button) {
                      var value = button[key];
                      if (value != null) {
                        if (value != '0' && param.button[key] != '0') {
                          _this.button[key] = value;
                        }
                      }
                      if (key == 'recall') {
                        _this.button[key] = param.button[key];
                      }
                    }
                  }
                }
                var bizPage = _this.instanceIdInfo.bizPage;
                if (bizPage.includes('?')) {
                  var str = bizPage.split('?');
                  bizPage = str[0];
                  _this.convertParam(str[1]);
                }
                var paramT = {
                  instanceInfo: _this.instanceIdInfo,
                  flowParam: _this.flowParam
                };
                _this.$nextTick(function () {
                  try {
                    yufp.router.to(bizPage, paramT, _this.nwfbiztypePage);
                  } catch (e) { }
                });
              } else {
                _this.$message({
                  duration: 6000,
                  message: '获取实例信息出错:' + response.message,
                  type: 'error'
                });
                _this.cancel();
              }
            }
          });
        },
        convertParam: function (params) {
          if (params.includes(',')) {
            params = params.split(',');
            for (var k = 0; k < params.length; k++) {
              var paramf = {};
              var item = params[k];
              paramf.key = item.split('=')[0];
              paramf.value = item.split('=')[1];
              var isHave = false;
              for (var i = 0; i < this.flowParam.length; i++) {
                if (this.flowParam[i].key === paramf.key) {
                  this.flowParam[i].value = paramf.value;
                  isHave = true;
                }
              }
              if (!isHave) {
                this.flowParam.push(paramf);
              }
            }
          } else {
            var paramf = {};
            paramf.key = params.split('=')[0];
            paramf.value = params.split('=')[1];
            var isHave = false;
            for (var i = 0; i < this.flowParam.length; i++) {
              if (this.flowParam[i].key === paramf.key) {
                this.flowParam[i].value = paramf.value;
                isHave = true;
              }
            }
            if (!isHave) {
              this.flowParam.push(paramf);
            }
          }
        },
        submitFn: function (opType) { // 功能方法实现接口
          var _this = this;
          // _this.submitbefore();
          if (opType == 'O-0') { // 拿回
            _this.$refs.commentForm.validate(function (vaild) { // 规则校验
              if (vaild) {
                _this.takeBackFn();
              } else {
                _this.$message({
                  duration: 6000,
                  message: '请检查输入项是否合法!',
                  type: 'warning'
                });
              }
            });
          } else if (opType == 'O-5') { // 催办
            _this.urgeFn();
          } else if (opType == 'O-3') { // 挂起
            _this.Hang();
          } else if (opType == 'O-4') { // 唤醒
            _this.Awake();
          } else if (opType == 'O-15') { // 撤回
            _this.$refs.commentForm.validate(function (vaild) { // 规则校验
              if (vaild) {
                _this.takeBackToFirstFn();
              } else {
                _this.$message({
                  duration: 6000,
                  message: '请检查输入项是否合法!',
                  type: 'warning'
                });
              }
            });
          }
        },
        cancel: function () { // 跳转到之前的页面
          yufp.router.to(this.returnBackFuncId, null, this.returnBackRootId);
        },
        // 挂起(发起人)
        Hang: function () { // 挂起操作
          var _this = this;
          _this.$confirm('确定要执行挂起操作吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            var param = {
              instanceId: _this.instanceIdInfo.instanceId
            };
            yufp.service.request({
              method: 'GET',
              data: param,
              url: _this.urls.hang,
              callback: function (code, message, response) {
                if (response.code == 0) { // 跳转到之前的页面
                  _this.$message({ message: response.data.tip + ';', type: 'success', duration: 6000 });
                  yufp.router.to(_this.returnBackFuncId, null, _this.returnBackRootId);
                } else {
                  _this.$message({ message: response.message + ';', type: 'error', duration: 6000 });
                }
              }
            });
          }).catch(function () { });
        },
        Awake: function () {
          var _this = this;
          _this.$confirm('确定要执行唤醒操作吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            var param = {
              instanceId: _this.instanceIdInfo.instanceId
            };
            yufp.service.request({
              method: 'GET',
              data: param,
              url: _this.urls.wake,
              callback: function (code, message, response) {
                if (response.code == 0) { // 跳转到之前的页面
                  _this.$message({ message: response.data.tip + ';', type: 'success', duration: 6000 });
                  yufp.router.to(_this.returnBackFuncId, null, _this.returnBackRootId);
                } else {
                  _this.$message({ message: response.message + ';', type: 'error', duration: 6000 });
                }
              }
            });
          }).catch(function () { });
        },
        // 拿回(已办)
        takeBackFn: function () {
          var _this = this;
          _this.$confirm('确定要拿回本条流程吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            var param = {};
            _this.commentInfo.commentSign = 'O-0';
            if (_this.commentInfo.userComment == '') {
              _this.commentInfo.userComment = '[拿回]';
            }
            param.comment = _this.commentInfo;
            param.orgId = _this.currentOrgId;
            yufp.service.request({
              method: 'POST',
              data: param,
              url: _this.urls.tackBack,
              callback: function (code, message, response) {
                if (response.code == 0) { // 跳转到之前的页面
                  if (response.data.code == '1') {
                    _this.$message({ message: response.data.tip + ';', type: 'error', duration: 6000 });
                  } else {
                    _this.$message({ message: response.data.tip + ';', type: 'success', duration: 6000 });
                  }
                  yufp.router.to(_this.returnBackFuncId, null, _this.returnBackRootId);
                } else {
                  _this.$message({ message: response.message + ';', type: 'error', duration: 6000 });
                }
              }
            });
          }).catch(function () { });
        },
        // 撤回(我的发起-已办)
        takeBackToFirstFn: function () {
          var _this = this;
          _this.$confirm('确定要将本条流程撤回到起始节点吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            var param = {};
            _this.commentInfo.commentSign = 'O-15';
            if (_this.commentInfo.userComment == '') {
              _this.commentInfo.userComment = '[撤回]';
            }
            param.comment = _this.commentInfo;
            param.orgId = _this.currentOrgId;
            yufp.service.request({
              method: 'POST',
              data: param,
              url: _this.urls.tackbackToFirst,
              callback: function (code, message, response) {
                if (response.code == 0) { // 跳转到之前的页面
                  _this.$message({ message: response.data.tip + ';', type: 'success', duration: 6000 });
                  yufp.router.to(_this.returnBackFuncId, null, _this.returnBackRootId);
                } else {
                  _this.$message({ message: response.message + ';', type: 'error', duration: 6000 });
                }
              }
            });
          }).catch(function () { });
        },
        // 催办(发起人)
        urgeFn: function () {
          var _this = this;
          _this.$confirm('确定要催促本条流程的办理进度吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            var param = {
              instanceId: _this.instanceIdInfo.instanceId,
              nodeId: _this.instanceIdInfo.nodeId
            };
            yufp.service.request({
              method: 'GET',
              data: param,
              url: _this.urls.urge,
              callback: function (code, message, response) {
                if (response.code == 0) { // 跳转到之前的页面
                  _this.$message({ message: response.data.tip + ';', type: 'success', duration: 6000 });
                  yufp.router.to(_this.returnBackFuncId, null, _this.returnBackRootId);
                } else {
                  _this.$message({ message: response.message + ';', type: 'error', duration: 6000 });
                }
              }
            });
          }).catch(function () { });
        },
        // 激活-我的发起-已办结
        activateFn: function () {
          var _this = this;
          _this.$confirm('确定要执行激活操作吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            var param = {
              instanceId: _this.instanceIdInfo.instanceId,
              currentUserId: yufp.session.userCode
            };
            yufp.service.request({
              method: 'GET',
              data: param,
              url: _this.urls.activate,
              callback: function (code, message, response) {
                if (response.code == 0) { // 跳转到之前的页面
                  _this.$message({ message: response.data.tip + ';', type: 'success', duration: 4000 });
                  yufp.router.to(_this.returnBackFuncId, null, _this.returnBackRootId);
                } else {
                  _this.$message({ message: response.message + ';', type: 'error', duration: 4000 });
                }
              }
            });
          }).catch(function () { });
        }
      }
    });
  };
});
